package Streamrecipient;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;

import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.Player;

public class RTPClient {
	
	private static final int RECEIVE_PORT = 1338;
	
	private Player player;
	private MediaLocator streamSource;
	private String errorMsg = null;
	private LinkedList<ActionListener> listeners;
	
	public RTPClient(){
		listeners = new LinkedList<ActionListener>();
	}
	
	public void listenToStream(String serverAddr){
		try {
			String srcUrl = "rtp://" + serverAddr + ":" + Integer.toString(RECEIVE_PORT) + "/audio";
			streamSource = new MediaLocator(srcUrl);
			
			player = Manager.createRealizedPlayer(streamSource);
			player.start();
		} catch (Exception e){
			errorMsg = e.getClass().toString();
			fireActionEvent(errorMsg);
		}
	}
	
	public void addActionListener(ActionListener al){
		listeners.add(al);
	}
	
	private void fireActionEvent(String msg){
		for(ActionListener l : listeners){
			l.actionPerformed(new ActionEvent(this,0, msg));
		}
	}
}
